<?php
/**
 * Created by PhpStorm.
 * User: x
 * Date: 17-7-27
 * Time: 上午9:20
 */

namespace Fanpaiji;


trait ReadExcel
{
    use FileFunc;

    private $dbConfig;

    function __construct()
    {
        $this->dbConfig = array(
            'DB_CONFIG' => array(
                //数据库配置
                'DB_HOST'=>'192.168.1.22',    //服务器地址
//                'DB_NAME' => '58car_58fabu', // 数据库名
                'DB_NAME' => 'vin', // 数据库名
                'DB_USER' => 'root', // 用户名
                'DB_PWD' => '123456', // 密码
                'DB_ENCODE'=>'utf8',//编码
                'DB_PORT' => '3306', //端口
                'DB_PREFIX' => '' // 数据库表前缀
            )
        );
    }


    /**
     * 读取 excel 表格数据
     * @param $filePath             文件路径
     * @param int $startRow         开始行数  从第几行开始读取
     * @param int $workNum          读取第几个工作文档数据
     * @param string $totalColumm   读取到第几列
     * @return array
     */
    public function read_data( $filePath , $startRow = 2, $workNum = 0, $totalColumm = "R" )//3597
    {
        if ( !$filePath ) {
            return ['status' => 1, 'messages' => "filePath is error", 'data' => []];
        }
        error_reporting(E_ALL);
        date_default_timezone_set('Asia/ShangHai');
        /** PHPExcel_IOFactory */
        require_once __DIR__.'/PHPExcel/IOFactory.php';
        // Check prerequisites
        if (!file_exists($filePath)) {
            return ['status' => 1, 'messages' => "not found {$filePath}", 'data' => []];
        }

        $reader = \PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
        $PHPExcel = $reader->load( $filePath ); // 载入excel文件
        $sheet = $PHPExcel->getSheet($workNum); // 读取第一個工作表

        $highestRow = $sheet->getHighestRow();// 取得总行数
        $highestColumm = $totalColumm ? $totalColumm : $sheet->getHighestColumn(); // 取得总列数

        $data = [];
        for ($row = $startRow; $row <= $highestRow; $row++){//行数是以第1行开始
            $dataset = [];
            for ( $columm = "A"; $columm <= $highestColumm; $columm++ ) {
                $dataset[ $sheet->getCell( $columm.'1' )->getValue() ] = $sheet->getCell( $columm.$row )->getValue();
            }
            $data[] = $dataset;
        }
        return ['status' => 0, 'message' => 'success', 'data' => $data];
    }

}